﻿@inject IProjectService ProjectService
<div align="center">
    <label>@_title</label>
</div>
<Chart @ref="_barChart" ChartType="ChartType.Bar" OnInitAsync="@OnInit" Height=300px />
@code {
    private Chart _barChart = default!;
    private List<ChartIntDataDto> chartDataDtos = new();
    private string _title = string.Empty;
    private int _year;
    protected override async Task OnInitializedAsync()
    {
        _year = DateTime.Today.Year;
        _title = $"{_year}年产品类型分布";
    }

    private async Task<ChartDataSource> OnInit()
    {
        chartDataDtos = await ProjectService.GetBlazorProductDistributionAsync(_year);
        var ds = new ChartDataSource();
        ds.Options.ShowLegend = false;
        ds.Options.X.Title = "Product";
        ds.Options.Y.Title = "Quantity";
        ds.Labels = chartDataDtos.Select(x => x.Label);
        ds.Data.Add(new ChartDataset
        {
            Label = "数量",
            Data = chartDataDtos.Select(x => x.Value).Cast<object>(),
        });
        return ds;
    }

}
